//
//  GonglueOfVC.m
//  project-A (pianke)
//
//  Created by dllo on 16/2/28.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "GonglueOfVC.h"
#import "CellOfDidian.h"
#import "RecipeCollectionheaderView.h"
#import <AFNetworking.h>
#import "ModleOfgonglue.h"
#import "VCOfCountry.h"
#import <DKNightVersion.h>

#define WIDTH [UIScreen mainScreen].bounds.size.width

#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface GonglueOfVC ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,retain)UICollectionViewFlowLayout *flowlayout;

@property(nonatomic,retain)UICollectionView *collection;

@property (nonatomic,assign)NSInteger number;

@property (nonatomic,retain)NSArray *arr;


@property (nonatomic,retain)NSArray *array;


@property (nonatomic,retain)NSMutableDictionary *dic;

@property (nonatomic,assign)BOOL recomplete;
@end

@implementation GonglueOfVC

-(void)dealloc{
    [_dic release];
    [_arr release];
    [_array release];
    [_flowlayout release];
    [_collection release];
    [super dealloc];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self geturlasd];
    
    [self creatcollection];
    self.number = 0;
    self.arr = @[@"   国外 · 亚洲",@"   国外 · 欧洲",@"   美洲 , 大洋洲 , 非洲与南极洲",@"   国内 · 港澳台",@"   国内 · 大陆"];
    self.collection.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
   
}


-(void)geturlasd{
    
    NSString *str = @"http://chanyouji.com/api/destinations.json";
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:str];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"阿萨大大的");
        } else {
            
            self.dic = [[NSMutableDictionary alloc]init];
            self.array = [NSMutableArray array];
            for (NSDictionary *dic in responseObject) {
//                                NSLog(@"%@",[dic objectForKey:@"category"]);
               NSMutableArray *array = [NSMutableArray array];
                
                               NSString *key = [dic objectForKey:@"category"];
                for (NSDictionary *dic2 in [dic objectForKey:@"destinations"]) {
//                    NSLog(@"%@",[dic2 objectForKey:@"name_zh_cn"]);
                    
                    ModleOfgonglue *model = [[ModleOfgonglue alloc]init];
                    
                    [model setValuesForKeysWithDictionary:dic2];
                    
                    model.idd = [NSString stringWithFormat:@"%@",[dic2 objectForKey:@"id"]];
                    
                    model.poi_count = [NSString stringWithFormat:@"%@",[dic2 objectForKey:@"poi_count"]];
                    
                    [array addObject:model];
                    
                    [model release];
                    
                }
                
                [self.dic setObject:array forKey:key];
                
            }
            
        
            self.array = [[self.dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
            
            NSLog(@"死亡如风%d",self.recomplete);
             [self.collection reloadData];
            self.recomplete = YES;
            
        }
        
    }];
    [dataTask resume];
    
    
}



-(void)creatcollection{
    
    [self createFlowLayout];
    
    self.collection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.flowlayout];
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    
    
    [self.view addSubview:self.collection];
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    self.collection.showsVerticalScrollIndicator = NO;
    
    
    [self.collection registerClass:[CellOfDidian class] forCellWithReuseIdentifier:@"pool1"];
    
    [self.collection registerClass:[RecipeCollectionheaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolForHeader"];
    
    
}

-(void)createFlowLayout{
    
    self.flowlayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.flowlayout.itemSize = CGSizeMake((WIDTH - 30)/2.0f,4 * (WIDTH - 30)/6.0f);
    
    self.flowlayout.minimumInteritemSpacing = 10;
    
    self.flowlayout.minimumLineSpacing = 10;
    
    self.flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.flowlayout.headerReferenceSize = CGSizeMake(WIDTH, 40);
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    NSString *ket = [self.array objectAtIndex:section];
    NSArray *arr = [self.dic objectForKey:ket];
        
    return arr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CellOfDidian *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor  redColor];
    
    if (self.recomplete == YES) {
        NSString *key = [self.array objectAtIndex:indexPath.section];
        NSArray *arr = [self.dic objectForKey:key];
        ModleOfgonglue *model = [arr objectAtIndex:indexPath.row];
        
        [cell passModel:model];
    }
    
    return cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld,%ld",indexPath.section,indexPath.item);
    if (self.recomplete == YES) {
        NSString *key = [self.array objectAtIndex:indexPath.section];
        NSArray *arr = [self.dic objectForKey:key];
        
        ModleOfgonglue *model = [arr objectAtIndex:indexPath.row];
        
        VCOfCountry *vc = [[VCOfCountry alloc]init];
        
        NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/%@.json",model.idd];
        
        NSLog(@"%@",url);
        
        vc.strurl  = url;
        
        [self.navigationController pushViewController:vc animated:YES];
        
        [vc release];
        
        
    }
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
   
    

    RecipeCollectionheaderView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"poolForHeader" forIndexPath:indexPath];
    
        head.backgroundColor = [UIColor cyanColor];
    
    head.label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:25];
    
      head.label.text = [self.arr objectAtIndex:indexPath.section];
      head.label.dk_textColorPicker  = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    head.label.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
    
        return head;
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 5;
}




-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
